/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmClinicListing.java
 *
 * Created on Aug 1, 2011, 6:32:09 PM
 */

package khmhappointmentsystem;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import org.jdesktop.application.Action;

/**
 *
 * @author mbelisle
 */
public class FrmClinicListing extends javax.swing.JInternalFrame {

    /** Creates new form FrmClinicListing */
    public FrmClinicListing() {
        initComponents();
        loadSpecialist();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cmbSpecialist = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        calDate = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Clinc Listings");

        jLabel1.setText("Display a report that list all the ");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Specialist:");
        jLabel2.setName("jLabel2"); // NOI18N

        cmbSpecialist.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbSpecialist.setName("cmbSpecialist"); // NOI18N

        jLabel3.setText("Clinic Date:");
        jLabel3.setName("jLabel3"); // NOI18N

        calDate.setName("calDate"); // NOI18N

        jLabel4.setText("appointments for the select specialist.");
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(FrmClinicListing.class, this);
        jButton1.setAction(actionMap.get("Cancel")); // NOI18N
        jButton1.setText("Cancel");
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("RunReport")); // NOI18N
        jButton2.setText("Run Report");
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(calDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbSpecialist, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton1))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbSpecialist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(calDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-253)/2, (screenSize.height-191)/2, 253, 191);
    }// </editor-fold>//GEN-END:initComponents

   void setCalendarDate()
    {
        calDate.setDate(new java.util.Date());
    }

   private void loadSpecialist()
    {
        cmbSpecialist.removeAllItems();
        cmbSpecialist.addItem(" ");
        String query = "SELECT DISTINCT specialist FROM clinic";
        try
        {
            PreparedStatement prep = Environment.getDbConn().prepareStatement(query);
            ResultSet rs = prep.executeQuery();

            while (rs.next())
            {
                cmbSpecialist.addItem(rs.getString(1));
            }
            rs.close();
            prep.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Action
    public void Cancel()
    {
        this.dispose();
    }

    @Action
    public void RunReport()
    {
        //create object for date
        DateFormat MDY_Formatter = new SimpleDateFormat("MM-dd-yyyy");

        String Date = MDY_Formatter.format(calDate.getDate());

        String report = "Clinic_List.jasper";
        String location = java.util.ResourceBundle.getBundle("khmhappointmentsystem.resources/Main").getString("reportFolderLocation");
        report = location + report;

        // Second, create a map of parameters to pass to the report.
        Map parameters = new HashMap();
        parameters.put("date", Date);

        if (cmbSpecialist.getSelectedIndex() == 0)
        {
            parameters.put("specialist", "%");
        }
        else
        {
            parameters.put("specialist", cmbSpecialist.getSelectedItem());
        }

        try
        {
            ReportFill ReportFill = new ReportFill();
            ReportFill.generateReport(report, parameters, Environment.getDbConn(), "Specialist Clinic Listings");
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser calDate;
    private javax.swing.JComboBox cmbSpecialist;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables

}
